package com.example.swagger.config;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;

/**
 *
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Bean//配置Swagger的docked实例
    public Docket docket(Environment environment){
        //获取项目的环境, pro还是dev
        Profiles profiles = Profiles.of("dev", "test");
        //判断是否处于上面的环境当中
        boolean envFlag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.apiInfo())
                .groupName("ray1")
                //.enable(false)则会被关闭
                .enable(envFlag)
                .select()
                //RequestHandlerSelectors, 配置要扫描接口的方式
                //basePackage: 指定要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
                //过滤哪些路径, 在该路径下的不被扫描
                //.paths(PathSelectors.ant("/example/**"))
                .build();
    }

    //把多个不同组的人放到bean中, 会在swagger-ui/index.html中多出几个选项
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("ray2");
    }

    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("ray3");
    }

    //作者信息
    private ApiInfo apiInfo(){
        Contact contact = new Contact("周睿", "", "zr18219@mail.ustc.edu.cn");
        return new ApiInfo("Swagger日志",
                "我的生命里, 不曾有你。",
                "v1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
